package yja.gzw.app.adapter

import android.content.Context
import android.content.Intent
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import yja.gzw.app.R
import yja.gzw.app.activity.DuntaixiangqingActivity
import yja.gzw.app.activity.TopshowActivity
import yja.gzw.app.adapter.PinglongAdapter.OnItemClickListener
import yja.gzw.app.fragmentview.TuijianFragment
import yja.gzw.app.model.UserData
import yja.gzw.app.model.bean.Commentpl
import yja.gzw.app.model.bean.QueryItem
import yja.gzw.app.viewmodel.Mainviewmodel

class TuijianAdapter(private val context: Context, private val list: List<QueryItem>,private val listener: OnItemClickListener): RecyclerView.Adapter<RecyclerView.ViewHolder>(){

   private val viewmodel= Mainviewmodel()
    private var view: View?=null
    private lateinit var neituada: NeituAdapter

    interface OnItemClickListener {
        fun onItemClick(position: Int, uri: QueryItem)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        view= LayoutInflater.from(parent.context).inflate(R.layout.item_view_rev,parent,false)
        Log.e("图片地址展示","走到了2")
        return ViewHodeView(view!!)
    }

    override fun getItemCount(): Int {

        Log.e("适配器大小",""+list.size)
        return list.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if(holder is ViewHodeView){
            val requestOptions = RequestOptions.bitmapTransform(CircleCrop())
            Glide.with(context).load("http://117ai.32.hzgqapp.com/"+list[position].user.avatar)
                .apply(requestOptions)
                .into(holder.touxiang)



            holder.neirun.text = list[position].content.toString()
            holder.biaoti.text = list[position].title.toString()

            holder.yulan.text = list[position].browse_count.toString()
            holder.pinglong.text = list[position].comment_count.toString()
            holder.dianzan.text = list[position].fav_count.toString()
            Log.e("点赞状态",list[position].is_fav_count.toString())
          val  listimg =list[position].images.split(",")


            holder.neiview.layoutManager = GridLayoutManager(context, 3) // 2 列
            neituada = NeituAdapter(context,listimg)
            holder.neiview.adapter = neituada


            if(list[position].is_fav_count==1){
                holder.dianzantu.setImageResource(R.mipmap.dianzanhou)

            }else{


                holder.dianzantu.setImageResource(R.mipmap.dianzan)

            }


            holder.dianzantu.setOnClickListener {
                listener.onItemClick(position,list[position])

//                holder.dianzan.text = (list[position].fav_count+1).toString()
//
//                viewmodel.ondianzanwang(UserData.token,mapOf("forum_id" to list[position].id.toString()))
//
//                viewmodel.ondianzan.observe(context as TopshowActivity, Observer{
//                    if(it.code==1){
//
//                        if(it.data.is_fav_count==1){
//                            holder.dianzantu.setImageResource(R.mipmap.dianzanhou)
//                            holder.dianzan.text = (list[position].fav_count).toString()
//                        }else{
//
//                            holder.dianzan.text = (list[position].fav_count-1).toString()
//                            holder.dianzantu.setImageResource(R.mipmap.dianzan)
//                        }
//
//
//                    }else{
//                        Toast.makeText(context,it.msg, Toast.LENGTH_SHORT).show()
//
//
//                    }
//
//                })

            }

            holder.itemView.setOnClickListener {

              //  Toast.makeText(context,list[position].id.toString(), Toast.LENGTH_SHORT).show()
                val intent=Intent(context, DuntaixiangqingActivity::class.java)
                intent.putExtra("forumid",list[position].id.toString())
                context.startActivity(intent)

            }


        }

    }


    inner class ViewHodeView(itemView: View):RecyclerView.ViewHolder(itemView){
        val neirun: TextView =itemView.findViewById(R.id.neirong)
        var neiview:RecyclerView=itemView.findViewById(R.id.neiview)
        val biaoti: TextView =itemView.findViewById(R.id.biaoti)
        val shijian: TextView =itemView.findViewById(R.id.shijian)
        var yulan:TextView=itemView.findViewById(R.id.yulanliang)
        var pinglong:TextView=itemView.findViewById(R.id.pinglongliang)
        var dianzan:TextView=itemView.findViewById(R.id.dianzanliang)
        var dianzantu:ImageView=itemView.findViewById(R.id.dianzan)
        val touxiang:ImageView=itemView.findViewById(R.id.touxiang)
    }


}